<?php

namespace app\admin\controller;

use app\admin\model\Store;

use think\Db;
use think\Exception;
use think\Request;

class Ticket extends Base
{

    public function getlist(){
        $params = $this->request->param();
        $where = [
            'status' => ['neq', 3]
        ];
        if(isset($params['name']) && $params['name']){
            $where['name'] = ['like', "%{$params['name']}%"];
        }

        $model = model('Ticket');
        $list = $model->where($where)->paginate(10);
        $total = $model->where($where)->count();

        if($list){
            foreach ($list as $item) {
                $item['storeName'] = Store::where('id', $item['storeId'])->value('name');
                /*switch ($item['status']) {
                    case 0:
                        $item['status'] = '未使用'; break;
                    case 1:
                        $item['status'] = '已使用'; break;
                    case 2:
                        $item['status'] = '已过期'; break;
                    case 4:
                        $item['status'] = '未发布'; break;
                    case 5:
                        $item['status'] = '已发布'; break;
                    default: break;
                }*/
            }
        }

        return $this->fetch('',[
            'datas' => $list,
            'total' => $total,
            'formget' => $_GET
        ]);
    }

    /**
     * 删除
     */
    public function del(Request $request){
        $model = model($this->model);
        if($request->isPost()){
            $param = $request->param();
            $id = $request->param('id');
            if(isset($_POST['ids'])){
                // 删除多条
                $ids = $param['ids'];
                $result = $model->whereIn('id', $ids)->update(['status' => 3]);
            }else if($id){
                // 删除一条
                $result = $model->where('id', $id)->update(['status' => 3]);
            }

            if($result === false){
                return show(config('status.error_code'),'删除失败','删除失败');
            }else{
                return show(config('status.success_code'),'删除成功','删除成功');
            }
        }
    }

}